<?php
class PageCache
{
	public static function frontOperation()
	{
		if(defined('PAGE_CACHE_ID'))
		{
			$cacheDir = PageCache::getCacheDir();
			$cach_file_path = $cacheDir.'/'.constant('PAGE_CACHE_ID');
			if(file_exists($cach_file_path))
			{
				echo file_get_contents($cach_file_path);
				exit();
			}else{
				//ob_start();
			}
		}
	}
	public static function endOperation()
	{
		if(defined('PAGE_CACHE_ID'))
		{
			$cacheDir = PageCache::getCacheDir();
			$cach_file_path = $cacheDir.'/'.constant('PAGE_CACHE_ID');
			$data = ob_get_contents();
			file_put_contents($cach_file_path,$data);
			ob_flush();
		}
	}
	public static function removeCache($cacheID = false)
	{
		$cacheDir = PageCache::getCacheDir();
		if($dh = opendir($cacheDir))
		{
			while(($file = readdir($dh)) !== false)
			{
				if($file == '.' || $file == '..')
					continue;
				if($cacheID === false)
				{
					unlink($cacheDir.'/'.$file);
				}else{
					if(strpos($file,$cacheID) === 0)
					{
						unlink($cacheDir.'/'.$file);
					}
				}
				
			}
		}
	}
	private static function getCacheDir()
	{
		$path = dirname(dirname(__FILE__)).'/_cache';
		if(!file_exists($path))
		{
			mkdir($path,0777,true);
		}
		return $path;
	}
}
?>